<?php


namespace App\Services;


class OttService
{
    const OTT_TEMPLATE_ORDER_NO_PAY = 'OTT_TEMPLATE_ORDER_NO_PAY';//待支付-表单类型
    const OTT_TEMPLATE_ORDER_SUCCESS_PAY = 'OTT_TEMPLATE_ORDER_SUCCESS_PAY';//支付成功-交易类型
    const OTT_TEMPLATE_ORDER_PAY_REFUND = 'OTT_TEMPLATE_ORDER_PAY_REFUND';//退款-交易类型
    const OTT_TEMPLATE_ORDER_OVERDUE = 'OTT_TEMPLATE_ORDER_OVERDUE';//逾期提醒-表单类型

    //跳转地址
    const OTT_JUMP_PAGE_INDEX = 'pages/index/index';//跳转到首页
    const OTT_JUMP_PAGE_INFO = 'pages/user/usercenter/usercenter';//跳转到个人中心

    protected static function getTplList(){
        $list = [
            'OTT_TEMPLATE_ORDER_NO_PAY'=>[
                config('alipay.template.orderNoPay') => [
                    'keyword1'=>['value'=>'keyword1'],//下单时间
                    'keyword2'=>['value'=>'keyword2'],//订单信息
                    'keyword3'=>['value'=>'keyword3'],//订单编号
                    'keyword4'=>['value'=>'keyword4'],//订单金额
                    'keyword5'=>['value'=>'您有一笔待支付订单，请您及时支付'],//支付提醒
                ],
                'page'=>self::OTT_JUMP_PAGE_INFO,
            ],
            'OTT_TEMPLATE_ORDER_SUCCESS_PAY'=>[
                config('alipay.template.orderSuccessPay') => [
                    'keyword1'=>['value'=>'keyword1'],//订单编号
                    'keyword2'=>['value'=>'keyword2'],//订单信息
                    'keyword3'=>['value'=>'keyword3'],//预定时间
                    'keyword4'=>['value'=>'keyword4'],//支付时间
                    'keyword5'=>['value'=>'keyword5'],//支付金额
                ],
                'page'=>self::OTT_JUMP_PAGE_INFO
            ],
            'OTT_TEMPLATE_ORDER_PAY_REFUND'=>[
                config('alipay.template.orderPayRefund') => [
                    'keyword1'=>['value'=>'keyword1'],//退款金额
                    'keyword2'=>['value'=>'keyword2'],//退款说明
                    'keyword3'=>['value'=>'keyword3'],//退款时间
                ],
                'page'=>self::OTT_JUMP_PAGE_INFO
            ],
            'OTT_TEMPLATE_ORDER_OVERDUE'=>[
                config('alipay.template.orderPayOverdue') => [
                    'keyword1'=>['value'=>'keyword1'],//订单号
                    'keyword2'=>['value'=>'keyword2'],//逾期天数
                    'keyword3'=>['value'=>'keyword3'],//逾期金额
                ],
                'page'=>self::OTT_JUMP_PAGE_INFO
            ],
        ];
        return $list;
    }


    protected static  function getTemplateParam($tplId,$param){
        if(empty($tplId)){
            return '';
        }
        $data = [];
        $list = self::getTplList();
        if(isset($list[$tplId])){
            $key = array_keys($list[$tplId]);
            $template_id = $key[0];
            if(isset($list[$tplId][$template_id])){
                $value = $list[$tplId][$template_id];
                if(!empty($param)){
                    foreach ($param as $k=>$item){
                        if(isset($value[$k])){
                            $value[$k]['value'] = $item;
                        }
                    }
                }
                $data = [
                    'user_template_id'=>$template_id,
                    'page'=>$list[$tplId]['page'],
                    'data'=>json_encode($value),
                ];
            }
        }
        return $data;
    }


    /**
     * 发送消息模板
     * @param $templateData
     * @return array
     */
    // public static function sendTemplateMsg($templateData){
    //     if(
    //         !isset($templateData['toUserId']) || empty($templateData['toUserId']) &&
    //         !isset($templateData['formId']) || empty($templateData['formId']) &&
    //         !isset($templateData['templateNo']) || empty($templateData['templateNo']) &&
    //         !isset($templateData['param']) || empty($templateData['param'])
    //     ){
    //         return ['status'=>false, 'message'=>'参数错误'];
    //     }
    //
    //     if(config('alipay.template.flag')){
    //         $data = self::getTemplateParam($templateData['templateNo'],json_decode($templateData['param'],true));
    //         if(!empty($data)){
    //             $sucdata = [
    //                 'to_user_id'=>$templateData['toUserId'],
    //                 'form_id'=>$templateData['formId'],
    //             ];
    //             return AlipayService::templateMessageSend(array_merge($sucdata,$data));
    //         }
    //     }
    //     return ['status'=>false, 'message'=>'暂无法发送'];
    // }

}
